<?php 
#!/usr/local/php/bin/php -q
/*
 * 短信脚本
*/

header('content-type:text/html;charset=utf-8');
define('DB_HOST','192.168.0.120');
define('DB_USER','kaka');
define('DB_PASS','kakameiye365');
define('DB_NAME','newmeiye');
define('DB_PORT',3306);
define('DB_CHAR','utf8');
define('APPNAME','');
/*
 * 短信通道 -- 叮咚
 */
define('B_MSG_CODE_URL',"http://121.42.160.216:6088/sms.aspx?action=send&userid=%s&account=%s&password=%s&mobile=%s&content=%s&sendTime=&extno=");
define('B_MSG_CODE_USERID','30');
define('B_MSG_CODE_ACCOUNT','wmh2016');
define('B_MSG_CODE_PASSWORD','wmh2016');

$conn=mysql_connect(DB_HOST.':'.DB_PORT,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);
mysql_query('set names ' . DB_CHAR);
$time = time()-36000;
//$time = 1486915200;

$sql = "SELECT * FROM action WHERE `status` != 2 AND  `use_end_time` BETWEEN ($time) AND ($time+432000) ";
$result = mysql_query($sql);


$array=array();
while($rows=mysql_fetch_assoc($result)){
    if($rows['use_end_time']==$time+431999){
        $actions[] = $rows;
        $actions_ids[] = $rows['id']; 
    }else{
        $array[]=$rows;
        $array_ids[] = $rows['id']; 
    }
}



$sql = "LOCK TABLES action_order WRITE";
$re=mysql_query($sql);

if($actions_ids){
    $actions_ids = implode(',', $actions_ids);
    $sql = " SELECT * FROM action_order WHERE action_id IN($actions_ids) ";
    $result = mysql_query($sql);
    while($rows=mysql_fetch_assoc($result)){  
        $arr[]=$rows;
    }
}

if($array_ids){
    $array_ids = implode(',', $array_ids);
    $sql = " SELECT * FROM action_order WHERE action_id IN($array_ids) AND `create_time`>($time-86400) ";
    $result = mysql_query($sql);
    while($rows=mysql_fetch_assoc($result)){  
        $arr[]=$rows;
    }
}


$sql = "UNLOCK TABLES"; 
$re=mysql_query($sql);

$action_id = 0;
foreach($arr as $k => $v){
    if($v['action_id'] != $action_id){
    
        $sql_s = "SELECT * FROM store WHERE `id` = ".$v['store_id'];
        $re_s=mysql_query($sql_s);
        $store=mysql_fetch_assoc($re_s);

        $sql_a = "SELECT * FROM action WHERE `id` = ".$v['action_id'];
        $re_a=mysql_query($sql_a);
        $action=mysql_fetch_assoc($re_a);
    }

    $_contents = '3&1&'.$store['name'].'&'.$action['action_name'].'&'.$action['use_end_time'].'&'.$store['tel'];

    $re = sendMsg($v['mobile'],$_contents);
    $action_id = $v['action_id'];
    echo (time().'----'.$v['mobile'].'----'.$re. "\r\n");
    
}






function achieve($url, $data, $timeout = 30) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $tmpInfo = curl_exec($curl);
    if (curl_errno($curl)) {
        echo 'Errno' . curl_error($curl);
    }
    curl_close($curl);
    return $tmpInfo;
}


function sendMsg( $mobile,$content = 0, $msgType = false ){

    // 确定选择哪个短信通道
    
    $msg_userid     = B_MSG_CODE_USERID;
    $msg_account    = B_MSG_CODE_ACCOUNT;
    $msg_password   = B_MSG_CODE_PASSWORD;
    $msg_url    	= B_MSG_CODE_URL;
    // 确定 短信内容
    $msg_content = sendContent( $content, $mobile );
    // 确定 请求URL
    $url = sprintf( $msg_url,$msg_userid,$msg_account,$msg_password,$mobile,$msg_content );
    $re = achieve($url);
    if(strstr($re,'Success')){
        return true;
    }else{
        return false;
    }
}

function sendContent( $content = 0 , $mobile = false ){
    $arr = explode( '&', $content );
    if ( $arr[0] == 3 ) {
        $content = '尊敬的顾客，您好！您在'.$arr[2].'购买的'.$arr[3].'订单即将过期，请您在'.date("Y-m-d",$arr[4]).'前到店消费，门店电话：'.$arr[5].'。【KAKA】';     
    }
    return urlencode( $content );

    /*
    ---3尊敬的顾客，您好！您在（门店）购买的（活动）订单即将过期，请您在【活动过期时间】前到店消费，门店电话：（电话）
   */
}





